home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / kant-generator-04-c / Kant ƒ / Kode ƒ / kant text twiddling.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  5.7 KB  |  249 lines  |  [TEXT/MMCC]

  1. #include "kant text twiddling.h"
  2. #include "kant main window.h"
  3. #include "kant parser.h"
  4. #include "program globals.h"
  5.  
  6. void SetTheText(Ptr data, long count)
  7. {
  8.     TEHandle            hTE;
  9.     
  10.     hTE=GetTheTextHandle();
  11.     TESetText(data, count, hTE);
  12.     TESetSelect(0, 0, hTE);
  13.     AdjustVScrollBar(gVScrollBar, hTE);
  14. }
  15.  
  16. TEHandle GetTheTextHandle(void)
  17. {
  18.     return GetWindowData_hTE(GetWindowDataHandle(kMainWindow), 0);
  19. }
  20.  
  21. Boolean AnyTextQQ(void)
  22. {
  23.     TEHandle        hTE;
  24.     
  25.     hTE=GetTheTextHandle();
  26.     return ((**hTE).teLength!=0);
  27. }
  28.  
  29. Boolean AnyHighlightedQQ(void)
  30. {
  31.     TEHandle        hTE;
  32.     
  33.     hTE=GetTheTextHandle();
  34.     return ((**hTE).selStart!=(**hTE).selEnd);
  35. }
  36.  
  37. void InsertTheText(Str255 theStr)
  38. {
  39.     TEHandle        hTE;
  40.     long            textLength;
  41.     
  42.     hTE=GetTheTextHandle();
  43.     textLength=theStr[0];
  44.     TEInsert(&theStr[1], textLength, hTE);
  45. }
  46.  
  47. short TotalNumberOfLines(TEHandle hTE)
  48. {
  49.     short            numLines;
  50.     
  51.     numLines=(**hTE).nLines;
  52.     if (*((unsigned char*)((long)(*((**hTE).hText))+(**hTE).teLength-1))==0x0d)
  53.         numLines++;
  54.     
  55.     return numLines;
  56. }
  57.  
  58. pascal void RawActionProc(ControlHandle theHandle, short partCode)
  59. {
  60.     short            scrollDistance;
  61.     TEHandle        hTE;
  62.     
  63.     hTE=GetTheTextHandle();
  64.     
  65.     switch (partCode)
  66.     {
  67.         case inUpButton:
  68.         case inDownButton:
  69.             scrollDistance=(**hTE).lineHeight;
  70.             break;
  71.         case inPageUp:
  72.         case inPageDown:
  73.             scrollDistance=(**hTE).viewRect.bottom-(**hTE).viewRect.top-(**hTE).lineHeight;
  74.             break;
  75.         default:
  76.             scrollDistance=0;
  77.             break;
  78.     }
  79.     
  80.     if ((partCode==inDownButton) || (partCode==inPageDown))
  81.         scrollDistance=-scrollDistance;
  82.     
  83.     MyMoveScrollBox(theHandle, scrollDistance/(**hTE).lineHeight);
  84.     
  85.     if (scrollDistance!=0)
  86.     {
  87.         TEPinScroll(0, scrollDistance, hTE);
  88.     }
  89. }
  90.  
  91. void MyMoveScrollBox(ControlHandle theControl, short scrollDistance)
  92. {
  93.     short            oldSetting, setting, max;
  94.     
  95.     oldSetting=GetControlValue(theControl);
  96.     max=GetControlMaximum(theControl);
  97.     setting=oldSetting-scrollDistance;
  98.     if (setting<0)
  99.         setting=0;
  100.     else if (setting>max)
  101.         setting=max;
  102.     
  103.     SetControlValue(theControl, setting);
  104. }
  105.  
  106. void AdjustVScrollBar(ControlHandle theControl, TEHandle hTE)
  107. {
  108.     short            oldValue, oldMax;
  109.     short            numLines, max, value;
  110.     
  111.     oldMax=GetControlMaximum(theControl);
  112.     oldValue=GetControlValue(theControl);
  113.     numLines=TotalNumberOfLines(hTE);
  114.     max=numLines-(((**hTE).viewRect.bottom-(**hTE).viewRect.top)/((**hTE).lineHeight));
  115.     if (max<0)
  116.         max=0;
  117.     SetControlMaximum(theControl, max);
  118.     value=((**hTE).viewRect.top-(**hTE).destRect.top)/((**hTE).lineHeight);
  119.     if (value<0)
  120.         value=0;
  121.     else if (value>max)
  122.         value=max;
  123.     SetControlValue(theControl, value);
  124. }
  125.  
  126. short CurrentLineNumber(TEHandle hTE)
  127. {
  128.     short            i;
  129.     short            currentEndPoint;
  130.     short            lineNumber;
  131.     short            numLines;
  132.     
  133.     currentEndPoint=(**hTE).selEnd;
  134.     lineNumber=0;
  135.     numLines=(**hTE).nLines;
  136.     for (i=0; i<numLines; i++)
  137.     {
  138.         if ((**hTE).lineStarts[i]<currentEndPoint)
  139.             lineNumber++;
  140.         else
  141.             return lineNumber;
  142.     }
  143.     
  144.     return numLines;
  145. }
  146.  
  147. pascal Boolean MyClikLoop(void)
  148. {
  149.     Point            thePoint;
  150.     TEHandle        hTE;
  151.     
  152.     hTE=(**GetWindowDataHandle(kMainWindow)).hTE[0];
  153.     GetMouse(&thePoint);
  154.     if (!PtInRect(thePoint, &((**hTE).viewRect)))
  155.     {
  156.         if (thePoint.v<((**hTE).viewRect.top))
  157.         {
  158.             if ((**hTE).selStart>0)
  159.                 TEPinScroll(0, (**hTE).lineHeight, hTE);
  160.         }
  161.         else if (thePoint.v>(**hTE).viewRect.bottom)
  162.         {
  163.             if ((**hTE).selEnd<(**hTE).teLength)
  164.                 TEPinScroll(0, -(**hTE).lineHeight, hTE);
  165.         }
  166.         
  167.         AdjustVScrollBar(gVScrollBar, hTE);
  168.     }
  169.     
  170.     return TRUE;
  171. }
  172.  
  173. void AdjustForEndScroll(ControlHandle theControl, TEHandle hTE)
  174. {
  175.     short            numLines;
  176.     short            offset;
  177.     
  178.     numLines=TotalNumberOfLines(hTE);
  179.     offset=numLines-GetControlMaximum(gVScrollBar)-
  180.         (((**hTE).viewRect.bottom-(**hTE).viewRect.top)/(**hTE).lineHeight);
  181.     if (offset<0)
  182.         TEPinScroll(0, -offset*((**hTE).lineHeight), hTE);
  183. }
  184.  
  185. /* Return a rectangle that is inset from the portRect by the size of
  186.     the scrollbars and a little extra margin. */
  187.  
  188. #define kTextMargin    2
  189.  
  190. void GetTERect(WindowPtr window, Rect *teRect)
  191. {
  192.     *teRect = window->portRect;
  193.     InsetRect(teRect, kTextMargin, kTextMargin);    /* adjust for margin */
  194.     teRect->bottom = teRect->bottom - 15;        /* and for the scrollbars */
  195.     teRect->right = teRect->right - 15;
  196. } /*GetTERect*/
  197.  
  198.  
  199. /* Update the TERec's view rect so that it is the greatest multiple of
  200.     the lineHeight that still fits in the old viewRect. */
  201.  
  202. void AdjustViewRect(TEHandle docTE)
  203. {
  204.     TEPtr        te;
  205.     
  206.     te = *docTE;
  207.     te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
  208.                             * te->lineHeight) + te->viewRect.top;
  209. } /*AdjustViewRect*/
  210.  
  211.  
  212. /*    Re-calculate the position and size of the viewRect and the scrollbars.
  213.     kScrollTweek compensates for off-by-one requirements of the scrollbars
  214.     to have borders coincide with the growbox. */
  215.  
  216. const short kScrollbarWidth = 16;
  217. const short kScrollbarAdjust = 15; // should be kScrollbarWidth - 1, but C is too stupid
  218. const short kScrollTweek = 2;
  219.  
  220. void AdjustScrollSizes(WindowPtr window, TEHandle hTE, ControlHandle vScrollBar,
  221.     ControlHandle hScrollBar)
  222. {
  223.     Rect        teRect;
  224.     
  225.     GetTERect(window, &teRect);                            /* start with TERect */
  226.     (**hTE).viewRect = teRect;
  227.     (**hTE).destRect.left=(**hTE).viewRect.left;
  228.     (**hTE).destRect.right=(**hTE).viewRect.right;
  229.     MoveControl(vScrollBar, window->portRect.right - kScrollbarAdjust, -1);
  230.     SizeControl(vScrollBar, kScrollbarWidth, (window->portRect.bottom - 
  231.                 window->portRect.top) - (kScrollbarAdjust - kScrollTweek));
  232.     MoveControl(hScrollBar, -1, window->portRect.bottom - kScrollbarAdjust);
  233.     SizeControl(hScrollBar, (window->portRect.right - 
  234.                 window->portRect.left) - (kScrollbarAdjust - kScrollTweek),
  235.                 kScrollbarWidth);
  236. } /*AdjustScrollSizes*/
  237.  
  238. void AdjustTE(TEHandle hTE, ControlHandle vScrollBar, ControlHandle hScrollBar)
  239. {
  240.     TEPtr        te;
  241.     
  242.     te = *hTE;
  243.     TEScroll((te->viewRect.left - te->destRect.left) -
  244.             GetControlValue(hScrollBar),
  245.             ((te->viewRect.top - te->destRect.top)/te->lineHeight) -
  246.                 GetControlValue(vScrollBar),
  247.             hTE);
  248. } /*AdjustTE*/
  249.